<?php
namespace core\helper;
/**
 * 验证统计类 自己定义的一些函数
 */
class Tools{
    
    /**
     * 生成指定长度的随机字符串
     */
    public static function rand($length,$type = 4){
        $chars = array(
            '1' => '0123456789',
            '2' => 'abcdefghijklnmopqrstuvwsyz',
            '3' => 'abcdefghijklnmopqrstuvwsyz0123456789',
            '4' => 'abcdefghijklnmopqrstuvwsyzABCDEFGHIJKLNMOPQRSTUVWSYZ',
            '5' => 'abcdefghijklnmopqrstuvwsyzABCDEFGHIJKLNMOPQRSTUVWSYZ0123456789',
            '6' => 'abcdefghijklnmopqrstuvwsyzABCDEFGHIJKLNMOPQRSTUVWSYZ0123456789`~!@#$%^&*()-_=+[]{}<>,.;:/?|',            
        );
        $str = $chars[$type];
        $len=strlen($str)-1;
        $randstr='';
        for($i=0;$i<$length;$i++) $randstr .= $str[mt_rand(0,$len)];
        return $randstr;
    }
    
    /**
    *	递归重组节点信息为多维数组
    *	@param [type] $node [要处理的节点数组]
    *	@param integer $pid [父级id]
    *   @param integer $pidname [父级id的字段名称]
    *	@param integer $access [权限数组]
    *	@param integer $nid [指定节点的与权限对应的字段]
    *   @param $backType 返回数据的数组类型 1 多维数组 2 一维数组[仅排序]
    *	@return [type]  [description] 
    */    
    public static function arrayMerge($node,$pidname='pid',$pid=0,$access=null,$nid='id',$backType = 1){
    	$arr=array();
    	foreach($node as $v){
    		if($v[$pidname]==$pid){
    			if(is_array($access)){
    				$v['isIn']=in_array($v['id'],$access) ? 'checked':'';
    			}
                if( $backType == 1){
                    $v['child']= self::arrayMerge($node,$pidname,$v['id'],$access,$nid);
    			    $arr[]=$v;
                }else{
                    $arr[]=$v;
                    $nextList = self::arrayMerge($node,$pidname,$v['id'],$access,$nid,$backType);                    
                    $arr = array_merge($arr,$nextList);                    
                }
    			
    		}
    	}    	
    	return $arr;
    }
    
    
}